﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace DevEx
{
    public partial class frmBackup : Form
    {
        public frmBackup()
        {
            InitializeComponent();
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void frmBackup_Load(object sender, EventArgs e)
        {
            //hiển thị tên mặc định cho file
            txtTenFile.Text = "QLVT_A41_" + DateTime.Now.Date.ToString("dd_MM_yyyy") + ".bak";

        }
        public static string GetGiaTriThanhPhan(string connectionString, string pName)
        {
            string KQ = "";
            string strTP = "";

            string[] data = connectionString.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i <= data.GetUpperBound(0); i++)
            {
                if (data[i].IndexOf(pName) > -1)
                {
                    strTP = data[i];
                    break;
                }
            }
            KQ = strTP.Replace(pName + "=", "");           
            return KQ;
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            //kiểm tra tên file và thư mục
            if (txtTenFile.Text == "")
            {
                MessageBox.Show("Nhập tên file backup đầy đủ!");
                txtTenFile.Focus();
                return;
            }
            if (txtFolder.Text == "")
            {
                MessageBox.Show("Chọn thư mục để lưu file!");
                txtFolder.Focus();
                return;
            }
            //tách chuỗi config để lấy các thông tin
            string connectionString = global::DevEx.Properties.Settings.Default.QLVTConnectionString;
            string databaseName = GetGiaTriThanhPhan(connectionString, "Initial Catalog");
            string serverName = GetGiaTriThanhPhan(connectionString, "Data Source");
            string userName = GetGiaTriThanhPhan(connectionString, "User ID");
            string password = GetGiaTriThanhPhan(connectionString, "Password");
            Backup sqlBackup = new Backup();

            sqlBackup.Action = BackupActionType.Database;
            sqlBackup.BackupSetDescription = "Backup QLVT_A41_" + DateTime.Now.Date.ToString("dd_MM_yyyy");
            sqlBackup.BackupSetName = "Archive";

            sqlBackup.Database = databaseName;

            BackupDeviceItem deviceItem = new BackupDeviceItem(txtFolder.Text + "\\" + txtTenFile.Text , DeviceType.File);
            ServerConnection connection = new ServerConnection(serverName, userName, password);
            Server sqlServer = new Server(connection);

            Database db = sqlServer.Databases[databaseName];

            sqlBackup.Initialize = true;
            sqlBackup.Checksum = true;
            sqlBackup.ContinueAfterError = true;

            sqlBackup.Devices.Add(deviceItem);
            sqlBackup.Incremental = false;

            sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
            sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

            sqlBackup.FormatMedia = false;

            sqlBackup.SqlBackup(sqlServer);
            MessageBox.Show("Backup thành công");
        }        
    }
}
